<template>
  <div class="tabs-head">
    <slot></slot>
    <div class="line" ref="line"></div>
    <!-- v-if会控制div是否显示在DOM里 -->
    <div class="actions-wrapper">
      <slot name="actions"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TouchTabsHead",
  inject: ["eventBus"],
  mounted() {
    this.eventBus.$on("update:selected", (item, vm) => {
      // console.log(vm.$el.offsetWidth);
      let width = vm.$el.offsetWidth;
      let left = vm.$el.offsetLeft;
      // let { width, left } = vm.$el.getBoundingClientRect();
      // console.log(width, left);
      this.$refs.line.style.width = `${width}px`;
      this.$refs.line.style.transform = `translateX(${left}px)`;
    });
  }
};
</script>

<style lang="scss" scoped>
$tab-height: 40px;
$blue: rgb(54, 191, 255);
$borderColor: #ddd;
.tabs-head {
  display: flex;
  justify-content: flex-start;
  position: relative;
  height: $tab-height;
  border-bottom: 1px solid $borderColor;
  > .line {
    position: absolute;
    bottom: 0;
    // width: 100px;
    border-bottom: 1px solid $blue;
    transition: all 300ms;
  }
  > .actions-wrapper {
    //设置margin-left auto，action就自动在最右边
    margin-left: auto;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
